﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.IO;
using System.Web.Routing;

namespace Framework.Library.Core.Utils.Optimizer.Handlers
{
    public class RewriteModule : IHttpHandlerFactory, IRequiresSessionState
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url1, string pathTranslated)
        {
            //string rewrite = context.Request.Url.AbsolutePath;
            //string newFilePath = rewrite.IndexOf("?") > 0 ? rewrite.Substring(0, rewrite.IndexOf("?")) : rewrite;
            ////Load & render page
            //HttpContext.Current.RewritePath(rewrite);
            //Page page = LoadPage(newFilePath, HttpContext.Current);
            //string cacheContent = RenderPage(page, HttpContext.Current);
            ////string cacheContent = "ok";
            //context.Response.Write(cacheContent);
            return context.Handler;
        }
        public void ReleaseHandler(IHttpHandler handler)
        {
        }
        public static Page LoadPage(string url, HttpContext context)
        {
            return (Page)PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
        }

        public static string RenderPage(Page page, HttpContext context)
        {
            StringWriter writer = new StringWriter();
            context.Server.Execute(page, writer, true);
            return writer.ToString().Trim();
        }
    }
}
